package GUI.Season;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import data.Season;
import data.Show;

public class SeasonController implements MouseListener, ActionListener
{

	private JFrame mainFrame;
	private SeasonTableModel tableModel;
	private JTable table;
	private JScrollPane tableScrollPane;
	private String frameName;
	private String[] columnString;
	private JPopupMenu rightClickMenu;
	private Season season;
	
	private final String SET_SELECT_EPISODE = "Set as Selected Episode";
	private final String SET_AS_DOWNLOADED = "Set as already downloaded";

	private Show show;
	private int seasonNum;

	public SeasonController(Show s, int seasonNum, String showName, Point startP) 
	{
		this.show = s;
		this.season = s.getSeason(seasonNum);
		this.seasonNum = seasonNum;
		String[] tempColumnNames = 
		{
				//"Selected",
				"Episode Number",
				"Episode Title",
				"Torrent URL",
				"Description",
				"Air Date"
		};
		this.columnString = tempColumnNames;
		this.frameName = showName + " - Season " +seasonNum;

		rightClickMenu = new JPopupMenu();
		
		JMenuItem rightClickMenuItem1 = new JMenuItem(SET_SELECT_EPISODE);
		rightClickMenuItem1.addActionListener(this);
		rightClickMenu.add(rightClickMenuItem1);
		
		JMenuItem rightClickMenuItem2 = new JMenuItem(SET_AS_DOWNLOADED);
		rightClickMenuItem2.addActionListener(this);
		rightClickMenu.add(rightClickMenuItem2);
		
		this.mainFrame = new JFrame(this.frameName);
		this.tableModel = new SeasonTableModel(this.getColumnNames(), this.season);
		this.table = new JTable(tableModel);
		this.table.addMouseListener(this);
		this.table.setDefaultRenderer(Object.class, new SeasonCellRenderer());
		this.tableScrollPane = new JScrollPane(table);
		this.mainFrame.add(tableScrollPane);
		this.mainFrame.setLocation(startP);
		new SeasonView(mainFrame);
	}

	
	public String[] getColumnNames()
	{
		return this.columnString;
	}

	private int getEpisodeNumberFromTable()
	{
		int episodeNumber = -1;
		Object obj = this.tableModel.getValueAt(table.getSelectedRow(), 0);
		if(obj instanceof String)
		{
			String dataString = (String) obj;
			//System.out.println(dataString);
			if(dataString.startsWith("Episode "))
			{
				dataString = dataString.replace("Episode ", "");
				episodeNumber = Integer.parseInt(dataString);
				//System.out.println(episodeNumber);
				//Point startP = new Point(this.mainFrame.getX()+this.mainFrame.getWidth(), this.mainFrame.getY());
				//new SeasonController(show.getSeason(seasonNumber), show.getShowName(), startP);
			}
		}		
		return episodeNumber;
	}

	@Override
	public void mouseClicked(MouseEvent e) 
	{
/*		if(SwingUtilities.isLeftMouseButton(e) && e.getSource() instanceof JPopupMenu)
		{
			
		}
		else if(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() >= 2)
		{
			getEpisodeNumberFromTable();
		}
		else 
		*/
		if(SwingUtilities.isRightMouseButton(e))
		{
			Point p = e.getPoint();
			int rowNumber = table.rowAtPoint(p);
			table.getSelectionModel().setSelectionInterval(rowNumber, rowNumber);
			rightClickMenu.show(e.getComponent(), e.getX(), e.getY());
		}
	}
	@Override
	public void mouseReleased(MouseEvent e) 
	{
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}


	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(SET_SELECT_EPISODE == e.getActionCommand())
		{
			int episodeNumber = this.getEpisodeNumberFromTable();
			this.show.setSelectedEpisode(this.seasonNum, episodeNumber);
			this.tableModel.rebuild(this.season);
			this.tableModel.fireTableDataChanged();
		}
		else if(SET_AS_DOWNLOADED == e.getActionCommand())
		{
			int episodeNumber = this.getEpisodeNumberFromTable();
			this.season.getEpisode(episodeNumber).setDownloaded(true);
			this.tableModel.rebuild(this.season);
			this.tableModel.fireTableDataChanged();
		}
	}



}
